//
//  MCLotteryRecordView.m
//  TLYL
//
//  Created by MC on 2017/6/21.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCLotteryRecordView.h"
#import "MCLotteryDrawDetailsViewController.h"
#import "UIView+MCParentController.h"
#import "MCMineCellModel.h"
#define WIDTH_LEFT  50
@interface MCLotteryRecordView ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray*marr_Section;
@property(nonatomic, strong)NSMutableArray*marr_title;
@property(nonatomic, strong)UIButton * btn_detail;
@property(nonatomic, strong)MCLotteryRecordHeaderView * view_header;
@end

@implementation MCLotteryRecordView

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    
    self.backgroundColor=RGB(238, 238, 238);
    _marr_Section= [[NSMutableArray alloc]init];

    [self addSubview:self.view_header];
    
    [self addSubview:self.btn_detail];
    
    [self addSubview:self.tableView];
    
    [self layOutConstraints];
    
    self.dataSource=nil;
}


-(void)layOutConstraints{

    [_view_header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(MC_REALVALUE(5));
        make.left.and.right.equalTo(self).offset(0);
        make.height.mas_equalTo([MCLotteryRecordHeaderView computeHeight:nil]);
    }];
    
    [_btn_detail mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.width.mas_equalTo(MC_REALVALUE(120));
        make.bottom.equalTo(self.mas_bottom).offset(-MC_REALVALUE(10));
        make.height.mas_equalTo(MC_REALVALUE(30));
    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_view_header.mas_bottom).offset(0);
        make.left.and.right.equalTo(self).offset(0);
        make.bottom.equalTo(_btn_detail.mas_top).offset(-MC_REALVALUE(10));
    }];

}

#pragma mark==============setDataSource=================
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
  
    NSMutableArray * marr_Model=[[NSMutableArray alloc]init];
    for(int i=0 ;i<10; i++){
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([MCLotteryRecordTableViewCell class]);
        model.className=NSStringFromClass([MCLotteryRecordTableViewCell class]);
        model.height = [MCLotteryRecordTableViewCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = self.marr_title[i];
        [marr_Model addObject:model];
    }
    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr_Model];
    model0.headerhHeight=0.0001;
    model0.footerHeight=0.0001;
    [_marr_Section addObject:model0];
    [_tableView reloadData];
    
}
#pragma mark-懒加载
-(UIButton *)btn_detail{
    if (!_btn_detail) {
        _btn_detail=[[UIButton alloc]init];
        _btn_detail.backgroundColor=RGB(48, 127, 207);
        [_btn_detail setTitle:@"查看完整期号" forState:UIControlStateNormal];
        [_btn_detail.titleLabel setFont:[UIFont systemFontOfSize:MC_REALVALUE(15)]];
        [_btn_detail addTarget:self action:@selector(action_detail) forControlEvents:UIControlEventTouchUpInside];
        [_btn_detail setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_detail.clipsToBounds=YES;
        _btn_detail.layer.cornerRadius=MC_REALVALUE(5);
    }
    return _btn_detail;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(MCLotteryRecordHeaderView *)view_header{
    if (!_view_header) {
        _view_header=[[MCLotteryRecordHeaderView alloc]init];
    }
    return _view_header;
}

#pragma mark-action_detail
-(void)action_detail{
    MCLotteryDrawDetailsViewController * vc = [[MCLotteryDrawDetailsViewController alloc]init];
    vc.fromClass=NSStringFromClass([[UIView MCcurrentViewController] class]);
    [[UIView MCcurrentViewController].navigationController pushViewController:vc animated:YES];
}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    return cm.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCLotteryRecordTableViewCell class])]) {
        
        MCLotteryRecordTableViewCell *ex_cell=(MCLotteryRecordTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        
    }
    return cell;
}


@end
#pragma mark-Cell
@interface MCLotteryRecordTableViewCell ()
@property (nonatomic,strong)UILabel *lab_left;
@property (nonatomic,strong)UILabel *lab_right;
@end

@implementation MCLotteryRecordTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    self.backgroundColor=[UIColor clearColor];
    UILabel * lab_left=[[UILabel alloc]init];
    [self addSubview:lab_left];
    _lab_left=lab_left;
    _lab_left.textAlignment=NSTextAlignmentLeft;
    _lab_left.text=@"加载中...";
    [_lab_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(WIDTH_LEFT);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    
    UILabel * lab_right=[[UILabel alloc]init];
    [self addSubview:lab_right];
    _lab_right=lab_right;
    _lab_right.text=@"加载中...";
    _lab_right.textAlignment=NSTextAlignmentRight;
    [_lab_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(-WIDTH_LEFT);
    }];
}
- (void)awakeFromNib {
    [super awakeFromNib];
}
    
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    
    _lab_left.text=@"0511023";
    _lab_right.text=@"8，6，8，6，2";
}

+(CGFloat)computeHeight:(id)info{
    return 30;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

@end
#pragma mark-Header
@interface MCLotteryRecordHeaderView ()
@end
@implementation MCLotteryRecordHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    UILabel * lab_left=[[UILabel alloc]init];
    [self addSubview:lab_left];
    lab_left.textAlignment=NSTextAlignmentLeft;
    lab_left.text=@"开奖期号";
    [lab_left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(WIDTH_LEFT);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    
    UILabel * lab_right=[[UILabel alloc]init];
    [self addSubview:lab_right];
    lab_right.text=@"开奖号码";
    lab_right.textAlignment=NSTextAlignmentRight;
    [lab_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(-WIDTH_LEFT);
    }];

    
}
+(CGFloat)computeHeight:(id)info{
    return 30;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
